//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.snowfeel.sfmall.promotion.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Stream;

public final class DateTimeUtil {
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String[][] WEEK_ARRAY = new String[][]{{"MONDAY", "一"}, {"TUESDAY", "二"}, {"WEDNESDAY", "三"}, {"THURSDAY", "四"}, {"FRIDAY", "五"}, {"SATURDAY", "六"}, {"SUNDAY", "日"}};

    public DateTimeUtil() {
    }

    public static String dateToString(Date date) {
        return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String dateToString(Date date, String format) {
        return DateFormatUtils.format(date, format);
    }

    public static Date stringToDate(String dateStr) {
        return stringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date stringToDate(String dateStr, String format) {
        if (!StringUtils.isEmpty(dateStr) && !StringUtils.isEmpty(format)) {
            try {
                return (new SimpleDateFormat(format)).parse(dateStr);
            } catch (ParseException var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }

    public static LocalDate getMonthStartDay(LocalDate date) {
        return Objects.isNull(date) ? null : date.withDayOfMonth(1);
    }

    public static LocalDate getCurMonthStartDay() {
        return getMonthStartDay(LocalDate.now());
    }

    public static LocalDate getDayBeforeDate(LocalDate date, int n) {
        return Objects.isNull(date) ? null : date.minusDays((long)n);
    }

    public static LocalDate getLatestSevenDay(LocalDate date) {
        return getDayBeforeDate(date, 6);
    }

    public static LocalDate getLatestSevenDayOfToday() {
        return getLatestSevenDay(LocalDate.now());
    }

    public static LocalDate stringToLocalDate(String dateStr, String format) {
        return !StringUtils.isEmpty(dateStr) && !StringUtils.isEmpty(format) ? LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(format)) : null;
    }

    public static LocalDate stringToLocalDate(String dateStr) {
        return stringToLocalDate(dateStr, "yyyy-MM-dd");
    }

    public static LocalDateTime stringToLocalDateTime(String dateStr, String format) {
        return !StringUtils.isEmpty(dateStr) && !StringUtils.isEmpty(format) ? LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(format)) : null;
    }

    public static LocalDateTime stringToLocalDateTime(String dateStr) {
        return stringToLocalDateTime(dateStr, "yyyy-MM-dd HH:mm:ss");
    }

    public static Long localDateToLong(LocalDate localDate) {
        return localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Date localDateToDate(LocalDate localDate) {
        return new Date(localDateToLong(localDate));
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return new Date(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
    }

    public static LocalDate dateToLocalDate(Date date) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
    }

    public static String localDateTimeToString(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String localDateToString(LocalDate localDate, String pattern) {
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String strLocalDateToString(String date, String pattern, String fomatPattern) {
        LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
        return localDateToString(localDate, fomatPattern);
    }

    public static LocalDateTime localDateToLocalDateTime(LocalDate localDate) {
        long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return localDateTime;
    }

    public static String localDateToStrLocalDateTime(LocalDate localDate, String pattern) {
        long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return localDateTimeToString(localDateTime, pattern);
    }

    public static LocalDate secondsLongToLocalDate(Long seconds) {
        return dateToLocalDate(new Date(seconds * 1000L));
    }

    public static List<LocalDate> getBetweenDate(LocalDate beginDate, LocalDate endDate) {
        List<LocalDate> list = new LinkedList();
        long distance = ChronoUnit.DAYS.between(beginDate, endDate);
        System.out.println(distance);
        if (distance < 1L) {
            list.add(beginDate);
        }

        Stream.iterate(beginDate, (d) -> {
            return d.plusDays(1L);
        }).limit(distance + 1L).forEach((f) -> {
            list.add(f);
        });
        return list;
    }

    public static Date getStartTime(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(11, 0);
        dateStart.set(12, 0);
        dateStart.set(13, 0);
        return dateStart.getTime();
    }

    public static Date getEndTime(Date date) {
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date);
        dateEnd.set(11, 23);
        dateEnd.set(12, 59);
        dateEnd.set(13, 59);
        return dateEnd.getTime();
    }

    public static List<LocalDate> getDateList(LocalDate start, LocalDate end) {
        List<LocalDate> result = new ArrayList();
        if (start != null && end != null && !start.isAfter(end)) {
            while(end.compareTo(start) >= 0) {
                result.add(start);
                start = start.plusDays(1L);
            }

            return result;
        } else {
            return result;
        }
    }

    public static String getDayOfTheWeek(LocalDate reservationDate) {
        String k = String.valueOf(reservationDate.getDayOfWeek());

        for(int i = 0; i < WEEK_ARRAY.length; ++i) {
            if (k.equals(WEEK_ARRAY[i][0])) {
                k = WEEK_ARRAY[i][1];
                break;
            }
        }

        return "周" + k;
    }

    public static Date getToday() {
        return localDateToDate(LocalDate.now());
    }

    public static Date yearAddNum(Date time, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(1, num);
        Date newTime = calendar.getTime();
        return newTime;
    }

    public static Date monthAddNum(Date time, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(2, num);
        Date newTime = calendar.getTime();
        return newTime;
    }

    public static Date dayAddNum(Date time, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(5, num);
        Date newTime = calendar.getTime();
        return newTime;
    }

    public static Date getMonthStartDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, 1);
        return calendar.getTime();
    }

    public static Date getMonthEndDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, calendar.getActualMaximum(5));
        return calendar.getTime();
    }

    public static Date getBeginWeekDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int dayofweek = cal.get(7);
        if (dayofweek == 1) {
            dayofweek += 7;
        }

        cal.add(5, 2 - dayofweek);
        return cal.getTime();
    }

    public static Date getEndWeekDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int dayofweek = cal.get(7);
        if (dayofweek == 1) {
            dayofweek += 7;
        }

        cal.add(5, 8 - dayofweek);
        return cal.getTime();
    }

    public static Date stampToDate(String s) {
        long lt = new Long(s);
        Date date = new Date(lt * 1000L);
        return date;
    }
}
